/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Tablero.java
 *
 * Created on 10/05/2011, 04:15:48 PM
 */
package logica;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author jorge
 */
public class Tablero extends javax.swing.JFrame implements Runnable, TreeSelectionListener {

    /** Creates new form Tablero */
    private JLabel tablero;
    public static ArrayList<usuario> jugadores;
    private usuario jugador_actual;
    private int pos_jugador = -1;
    private int cantidadDobles = 0;
    private int min = 0, seg = 0;
    private propiedad[] props;
    private Thread reloj;
    public static int res_Dados;
    public String[] sidedado = {"/img/1.png", "/img/2.png", "/img/3.png", "/img/4.png", "/img/5.png", "/img/6.png"};
    private URL side1 = null;
    private URL side2 = null;
    private ImageIcon i = null;
    private URL cosa = getClass().getResource("/img/doncito.png");
    private ImageIcon doncito = new ImageIcon();
    private PrintStream salida;
    private String fin="";

    public Tablero(ArrayList jugadores) {
        props = new propiedad[40];
        this.jugadores = jugadores;
        initComponents();
        inicializarTablero();
        inicializarjugadores();
        cambiarTurno(true);

        setIconImage(new ImageIcon(getClass().getResource("/img/doncito.png")).getImage());
        setLocationRelativeTo(null);
        this.jTextField1.setVisible(false);
        this.jTextField2.setVisible(false);
        reloj = new Thread(this);
        reloj.start();
    }

    private void inicializarjugadores() {
        DefaultMutableTreeNode padre = new DefaultMutableTreeNode("Jugadores");
        DefaultTreeModel modelo = new DefaultTreeModel(padre);
        this.jTree1.setModel(modelo);
        DefaultMutableTreeNode hijo;
        for (int i = 0; i < jugadores.size(); i++) {
            hijo = new DefaultMutableTreeNode(jugadores.get(i).getNombre());
            modelo.insertNodeInto(hijo, padre, i);
        }
        jTree1.addTreeSelectionListener(this);

    }

    public void valueChanged(TreeSelectionEvent evt) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
        String texto = node.toString();
        usuario aux = null;
        for (int i = 0; i < this.jugadores.size(); i++) {
            aux = jugadores.get(i);
            if (jugadores.get(i).getNombre().equals(texto)) {
                txtNombre.setText(jugadores.get(i).getNombre());
                txtDinero.setText(Float.toString(jugadores.get(i).getDinero()));
                txtDeuda.setText(Float.toString(jugadores.get(i).getDeuda()));
                txtTiros.setText(Integer.toString(jugadores.get(i).getTiros()));
                break;
            }
        }
        if (aux != null) {
            DefaultTableModel modelo1 = new DefaultTableModel(new String[]{"Id propiedad", "Nombre", "Precio", "Numero de casas", "Tiene hotel", "Esta hipotecada", "Color"}, 0);
            propiedad tmp = null;
            for (int i = 0; i < aux.getPropiedades().size(); i++) {
                tmp = aux.getPropiedades().get(i);
                modelo1.addRow(new Object[]{tmp.getId(), tmp.getDescripcion(), tmp.getPrecio(), tmp.getTipo() == propiedad.TIPO_LOTE ? ((lote) tmp).getNumCasas() : "-", tmp.getTipo() == propiedad.TIPO_LOTE ? ((lote) tmp).isHotel() : "-", tmp.getTipo() == propiedad.TIPO_LOTE ? ((lote) tmp).isHipotecada() : "-", tmp.getTipo() == propiedad.TIPO_LOTE ? ((lote) tmp).getColorTexto() : "-"});
            }
            jTable1.setModel(modelo1);
        }

    }

    private void inicializarTablero() {
        tablero = new JLabel();
        tablero.setBounds(0, 0, 680, 642);
        for (int i = 0; i < jugadores.size(); i++) {
            jugadores.get(i).getFicha().setBounds(600, 570, 50, 50);
            jugadores.get(i).setPros(props);
            jPanel1.add(jugadores.get(i).getFicha());
        }
        tablero.setIcon(new ImageIcon(getClass().getResource("/img/Tablero.jpg")));
        jPanel1.add(tablero);
        props[1] = new lote(1, 40, 10, "Ronda de valencia", Color.MAGENTA);
        props[3] = new lote(2, 60, 20, "Plaza layapies", Color.MAGENTA);
        props[5] = new estacion(3, "Estacion de goya", 200);
        props[6] = new lote(4, 100, 30, "Glorieta cuatro caminos", Color.CYAN);
        props[8] = new lote(5, 100, 40, "Avenida de reina victoria", Color.CYAN);
        props[9] = new lote(6, 120, 50, "Calle bravo murillo", Color.CYAN);
        props[11] = new lote(7, 140, 100, "Glorieta de Bilvao", Color.PINK);
        props[12] = new servicio(8, "Compañia de electricidad", 150);
        props[13] = new lote(9, 150, 120, "Calle Alberto Aguilera", Color.PINK);
        props[14] = new lote(10, 160, 130, "Calle Fuencarral", Color.PINK);
        props[15] = new estacion(11, "Estacion de las delicias", 200);
        props[16] = new lote(12, 180, 150, "AVDA Felipe II", Color.ORANGE);
        props[18] = new lote(13, 180, 150, "Calle Velázquez", Color.ORANGE);
        props[19] = new lote(14, 200, 160, "Calle Serrano", Color.ORANGE);
        props[21] = new lote(15, 220, 180, "Avenida de América", Color.RED);
        props[23] = new lote(16, 220, 180, "Calle Maria de Molina", Color.RED);
        props[24] = new lote(17, 240, 200, "Calle Cea Bermúdez", Color.RED);
        props[25] = new estacion(18, "Estación del medio día", 200);
        props[26] = new lote(19, 260, 220, "Avenida de los Reyes Católicos", Color.YELLOW);
        props[27] = new lote(20, 260, 220, "Calle Belén", Color.YELLOW);
        props[28] = new servicio(21, "compañía distribución agua", 150);
        props[29] = new lote(22, 280, 235, "Plaza de España", Color.YELLOW);
        props[31] = new lote(23, 300, 250, "Puerta del sol", Color.GREEN);
        props[32] = new lote(24, 300, 250, "Calle de Alcalá", Color.GREEN);
        props[34] = new lote(25, 320, 280, "Gran Vía", Color.GREEN);
        props[35] = new estacion(26, "Estación del Norte", 200);
        props[37] = new lote(27, 350, 300, "Paseo de la castellana", Color.BLUE);
        props[39] = new lote(28, 400, 250, "Paseo del prado", Color.BLUE);

    }

    private void cambiarTurno(boolean cambiar) {
        if (cambiar) {
            if (pos_jugador == jugadores.size() - 1) {
                pos_jugador = 0;
            } else {
                pos_jugador++;
            }
            jugador_actual = jugadores.get(pos_jugador);
        }
        if (jugador_actual.isPreso()) {
            if (jugador_actual.isPase()) {
                int opcion1 = JOptionPane.showConfirmDialog(null, "Se encuentra preso y posee un pase ¿desea utilizarlo?", "Atencion", JOptionPane.INFORMATION_MESSAGE);
                if (opcion1 == JOptionPane.OK_OPTION) {
                    jugador_actual = jugadores.get(pos_jugador);
                    jugador_actual.setPreso(false);
                    jugador_actual.setTurnos_perdidos(0);
                    jLabel4.setText(jugador_actual.getNombre() + "    " + Float.toString(jugador_actual.getDinero()) + "euros");
                    jugador_actual.setPase(false);
                    return;
                }
            }
            int opcion = JOptionPane.showConfirmDialog(null, "Se encuentra preso desea pagar 50 euros para continuar", "Atencion", JOptionPane.INFORMATION_MESSAGE);
            if (opcion == JOptionPane.OK_OPTION) {
                jugador_actual = jugadores.get(pos_jugador);
                jugador_actual.setPreso(false);
                jugador_actual.setTurnos_perdidos(0);
            } else {
                jugador_actual.gastarPenitencia();
                cambiarTurno(true);
                return;
            }

        } else if (jugador_actual.getTurnos_perdidos() > 0) {
            jugador_actual.gastarPenitencia();
            cambiarTurno(true);
            return;
        }

        jLabel4.setText(jugador_actual.getNombre() + "    " + Float.toString(jugador_actual.getDinero()) + "euros");
    }

    private void mandarPreso() {
        jugador_actual.setPosicion(10);
        jugador_actual.setPreso(true);
        jugador_actual.setTurnos_perdidos(3);
    }

    private void moverPieza(int cantidad) {
        if (cantidad + jugador_actual.getPosicion() > 39) {
            jugador_actual.setPosicion(jugador_actual.getPosicion() + cantidad - 39);
            jugador_actual.setDinero(jugador_actual.getDinero() + 200);
            JOptionPane.showMessageDialog(null, "Se han abonado 200 euros a su cuenta por biáticos", "Atención", JOptionPane.INFORMATION_MESSAGE);
        } else {
            jugador_actual.setPosicion(jugador_actual.getPosicion() + cantidad);
        }
        jugador_actual.posicionar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jside1 = new javax.swing.JLabel();
        jside2 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtDeuda = new javax.swing.JTextField();
        txtDinero = new javax.swing.JTextField();
        txtTiros = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(null);

        setIconImage(new ImageIcon(getClass().getResource("/img/doncito.png")).getImage());
        jInternalFrame1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jInternalFrame1.setIconifiable(true);
        jInternalFrame1.setTitle("Tiro");
        jInternalFrame1.setVisible(true);

        jButton1.setText("Tirar dados");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jside1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/1.png"))); // NOI18N

        jside2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/2.png"))); // NOI18N

        jTextField7.setEditable(false);

        jLabel7.setText("Tiempo de juego");

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addComponent(jside1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jside2)
                        .addGap(18, 18, 18)
                        .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton1))
                .addContainerGap())
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addGap(11, 11, 11)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jside2)
                        .addComponent(jside1)
                        .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(39, 39, 39)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.add(jInternalFrame1);
        jInternalFrame1.setBounds(140, 70, 200, 220);
        jInternalFrame1.setFrameIcon(doncito);

        jTabbedPane1.addTab("Tablero del juego", jPanel1);

        jSplitPane1.setDividerLocation(200);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Jugadores");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("colors");
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("sports");
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("food");
        treeNode1.add(treeNode2);
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(jTree1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
        );

        jSplitPane1.setLeftComponent(jPanel2);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Informacion general"));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Nombre:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Dinero:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Deuda:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("Tiros:");

        txtNombre.setEditable(false);

        txtDeuda.setEditable(false);

        txtDinero.setEditable(false);

        txtTiros.setEditable(false);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                        .addComponent(txtTiros, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addComponent(txtDeuda, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                        .addComponent(txtDinero, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(33, 33, 33)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(55, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtDinero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtDeuda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtTiros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Propiedades"));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Id propiedad", "Nombre", "Precio", "Numero de casas", "Tiene hotel", "Está hipotecada"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jSplitPane1.setRightComponent(jPanel3);

        jTabbedPane1.addTab("Informacion de jugadores", jSplitPane1);

        jMenu1.setText("Juego");

        jMenu3.setText("Acciones");

        jMenuItem3.setText("Comprar casa/hotel");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuItem5.setText("Vender propiedad");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem5);

        jMenuItem6.setText("Pagar hipoteca");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem6);

        jMenu1.add(jMenu3);

        jMenuItem2.setText("Salir");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu4.setText("Estadisticas");

        jMenuItem4.setText("Usuario final");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem4);

        jMenuItem7.setText("Usuario administrador");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem7);

        jMenuBar1.add(jMenu4);

        jMenu2.setText("Ayuda");

        jMenuItem1.setText("Acerca de");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 695, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int dado1 = (int) (Math.random() * 6 + 1);
        int dado2 = (int) (Math.random() * 6 + 1);
        jugador_actual.setTiros(jugador_actual.getTiros() + 1);
        res_Dados = dado1 + dado2;
        this.jTextField1.setText(Integer.toString(dado1));
        side1 = getClass().getResource(sidedado[dado1 - 1]);
        i = new ImageIcon(side1);
        jside1.setIcon(i);
        this.jTextField2.setText(Integer.toString(dado2));
        side2 = getClass().getResource(sidedado[dado2 - 1]);
        i = new ImageIcon(side2);
        jside2.setIcon(i);
        if (dado1 != dado2) {
            moverPieza(dado1 + dado2);
            cambiarTurno(true);
            cantidadDobles = 0;

        } else {
            cantidadDobles++;
            if (cantidadDobles == 3) {
                mandarPreso();
                cantidadDobles = 0;
                cambiarTurno(true);
            } else {
                moverPieza(dado1 + dado2);
                cambiarTurno(false);
            }
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        acercade ac = new acercade(this, true);
        ac.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        // TODO add your handling code here:
        new PagarHipoteca(jugadores).setVisible(true);
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        new comprarCasaHotel(jugadores, props).setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        // TODO add your handling code here:
        new venderPropiedad(jugadores).setVisible(true);
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // TODO add your handling code here:
        new SeleccionUsuario(jugadores).setVisible(true);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        // TODO add your handling code here:
        new login().setVisible(true);
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTree jTree1;
    private javax.swing.JLabel jside1;
    private javax.swing.JLabel jside2;
    private javax.swing.JTextField txtDeuda;
    private javax.swing.JTextField txtDinero;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtTiros;
    // End of variables declaration//GEN-END:variables

    public void run() {
        while (true) {
            try {
                seg++;
                if (seg >= 60) {
                    min++;
                    seg = 0;
                }
                if (min == 15) {
                    try {
                        String esc=escribir();
                        salida = new PrintStream(new File("datos.txt"));
                        salida.print(esc);
                        int puesto = 1;
                        float dinero1 = 0, dinero2 = 0;
                        for (int i = 0; i < jugadores.size(); i++) {
                            puesto = 1;
                            dinero1 = jugadores.get(i).getDinero() - jugadores.get(i).getDeuda();
                            for (int k = 0; k < jugadores.get(i).getPropiedades().size(); k++) {
                                if (jugadores.get(i).getPropiedades().get(k).tipo == propiedad.TIPO_LOTE) {
                                    if (!((lote) jugadores.get(i).getPropiedades().get(k)).isHipotecada()) {
                                        dinero1 += jugadores.get(i).getPropiedades().get(k).getCosto();
                                    }
                                } else {
                                    dinero1 += jugadores.get(i).getPropiedades().get(k).getCosto();
                                }
                            }
                            for (int j = 0; j < jugadores.size(); j++) {
                                if (i != j) {
                                    dinero2 = jugadores.get(j).getDinero() - jugadores.get(j).getDeuda();
                                    for (int k = 0; k < jugadores.get(j).getPropiedades().size(); k++) {
                                        if (jugadores.get(j).getPropiedades().get(k).tipo == propiedad.TIPO_LOTE) {
                                            if (!((lote) jugadores.get(j).getPropiedades().get(k)).isHipotecada()) {
                                                dinero2 += jugadores.get(j).getPropiedades().get(k).getCosto();
                                            }
                                        } else {
                                            dinero2 += jugadores.get(j).getPropiedades().get(k).getCosto();
                                        }
                                    }
                                    if(dinero1<dinero2)
                                        puesto++;
                                }
                            }
                            salida.print(jugadores.get(i).getNombre()+",");
                            salida.print(puesto);
                            salida.print(",");
                            Calendar cal = Calendar.getInstance();
                            salida.print(cal.getTime().getDate());
                            salida.print(",");
                            salida.print(cal.getTime().getMonth()+1);
                            salida.print(",");
                            salida.print(cal.getTime().getYear()+1900);
                            salida.print(",");
                            salida.println((int)dinero1);
                            fin+=jugadores.get(i).getNombre()+" puesto: "+puesto+"\n";
                        }
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(Tablero.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    JOptionPane.showMessageDialog(null, fin, "Atención", JOptionPane.INFORMATION_MESSAGE);
                    break;
                }
                this.jTextField7.setText((min < 10 ? "0" + Integer.toString(min) : Integer.toString(min)) + ":" + (seg < 10 ? "0" + Integer.toString(seg) : Integer.toString(seg)));
                Thread.sleep(1000);
            } catch (InterruptedException ev) {
            }
        }
        salida.close();
        dispose();
    }
    private String escribir(){
        String textp="";
        try {
            BufferedReader entrada = new BufferedReader(new FileReader("datos.txt"));
            while(entrada.ready()){
                textp+=entrada.readLine()+"\n";
            }
            entrada.close();
        } catch (Exception ex) {
            
        }
        return textp;
    }
}
